package com.house.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.house.common.pojo.EUDataGridResult;
import com.house.excel.ExcelToList;
import com.house.pojo.Houseuser;
import com.house.service.HouseuserService;

@Controller
public class HouseuserController {

	@Autowired
	private HouseuserService houseuserService;

	@RequestMapping("/user/list")
	@ResponseBody
	public EUDataGridResult getUserList(Integer page, Integer rows) {
		EUDataGridResult result = houseuserService.getUserList(page, rows);
		return result;
	}

	@RequestMapping("/user/save")
	@ResponseBody
	public String addUser(Houseuser test) {
		houseuserService.add(test);
		System.out.println(test.getUserTime());

		return "success";
	}

	@RequestMapping("/user/delete")
	@ResponseBody
	public String deleteUser(String id) {
		String[] ids = id.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for (String i : ids) {
			list.add(Integer.parseInt(i));
		}
		houseuserService.delete(list);
		return "success";
	}
	
	@RequestMapping("/user/saveExcel")
	@ResponseBody
	public String addExcelUser( MultipartFile uploadFile ) throws Exception{
		
		System.out.print(uploadFile.getOriginalFilename());
		
		InputStream inp = uploadFile.getInputStream();
		
		
		houseuserService.addExcle(ExcelToList.readWorkBook(inp));
		
/*		Houseuser user1 = new Houseuser();
		user1.setUserName("8888");
		user1.setUserId("111");
		user1.setUserAddress("222");		

		List<Houseuser> list = new ArrayList<Houseuser>();
		list.add(user1);

		houseuserService.addExcle(list);*/
		return "success";
	}
}
